package dataselect.soilwatersensor;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.JTableHeader;

public class SensorManage extends JFrame {

    private JPanel contentPane;
	private static JTextField textField_0;// 分组数量
	private static JTextField textField_1;// 传感器数量
	private static JTextField textField_a;// 二次项系数值
	private static JTextField textField_b;// 一次项系数值
	private static JTextField textField_c;// 常数项系数值
	private static ArrayList idList = new ArrayList();// 传感器编号线性数组
	private static ArrayList groupList = new ArrayList();// 分组编号线性数组
	private static ArrayList depthList = new ArrayList();// 深度线性数组
	public static final String SOILVWATER = "土壤体积含水量量传感器",
			SOILWJZS = "土壤水基质势传感", SOILWSALT = "土壤水盐传感器";
	private static String type; // 传感器类型

	/**
	 * Launch the application.
	 */

	/**
	 * Create the frame. 传感器设置
	 */
	public SensorManage(String type) {
		this.type = type;
		setTitle("传感器类型 - " + type);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 452, 459);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel label = new JLabel("传感器分组设置");
		label.setFont(new Font("华文宋体", Font.BOLD, 16));
		label.setBounds(160, 10, 119, 21);
		contentPane.add(label);

		JLabel label_1 = new JLabel("请输入以下信息：");
		label_1.setFont(new Font("华文宋体", Font.PLAIN, 16));
		label_1.setBounds(10, 47, 137, 15);
		contentPane.add(label_1);

		JLabel lblNewLabel = new JLabel("传感器数量：");
		lblNewLabel.setFont(new Font("华文宋体", Font.PLAIN, 15));
		lblNewLabel.setBounds(94, 92, 90, 15);
		contentPane.add(lblNewLabel);

		textField_0 = new JTextField();
		textField_0.setBounds(213, 85, 119, 29);
		contentPane.add(textField_0);
		textField_0.setColumns(10);

		JLabel lblNewLabel_1 = new JLabel("传感器组数：");
		lblNewLabel_1.setFont(new Font("华文宋体", Font.PLAIN, 15));
		lblNewLabel_1.setBounds(94, 149, 90, 15);
		contentPane.add(lblNewLabel_1);

		textField_1 = new JTextField();
		textField_1.setBounds(213, 137, 119, 29);
		contentPane.add(textField_1);
		textField_1.setColumns(10);

		JLabel lblNewLabel_a = new JLabel("二次项系数a:");
		lblNewLabel_a.setFont(new Font("华文宋体", Font.PLAIN, 15));
		lblNewLabel_a.setBounds(94, 201, 90, 15);
		contentPane.add(lblNewLabel_a);

		textField_a = new JTextField();
		textField_a.setBounds(213, 189, 119, 29);
		contentPane.add(textField_a);
		textField_a.setColumns(10);

		JLabel lblNewLabel_b = new JLabel("一次项系数b:");
		lblNewLabel_b.setFont(new Font("华文宋体", Font.PLAIN, 15));
		lblNewLabel_b.setBounds(94, 246, 90, 15);
		contentPane.add(lblNewLabel_b);

		textField_b = new JTextField();
		textField_b.setBounds(213, 238, 119, 32);
		contentPane.add(textField_b);
		textField_b.setColumns(10);

		JLabel lblNewLabel_c = new JLabel("常数项C:");
		lblNewLabel_c.setFont(new Font("华文宋体", Font.PLAIN, 15));
		lblNewLabel_c.setBounds(112, 294, 79, 15);
		contentPane.add(lblNewLabel_c);

		textField_c = new JTextField();
		textField_c.setBounds(213, 286, 119, 29);
		contentPane.add(textField_c);
		textField_c.setColumns(10);

		JLabel label_2 = new JLabel("输入后请点击->");
		label_2.setFont(new Font("华文宋体", Font.PLAIN, 14));
		label_2.setForeground(Color.RED);
		label_2.setBounds(94, 341, 109, 15);
		contentPane.add(label_2);

		JButton button = new JButton("分组与设置");
		button.setFont(new Font("华文宋体", Font.PLAIN, 13));
		button.setBackground(Color.WHITE);
		button.setForeground(Color.BLUE);
		button.setBounds(213, 336, 119, 23);
		contentPane.add(button);
		setVisible(true);
		button.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				groupAndSensor();
			}
		});
	}

	// 分组编号和传感器编号映射
	public static void groupAndSensor() {
		String[][] rowData = new String[200][3];
		String[] columnData = { "分组编号(共" + textField_1.getText() + "组)",
				"传感器编号(共" + textField_0.getText() + "个)", "传感器埋设深度" };

		JFrame frame1 = new JFrame("传感器分组设置");
		frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel tJPanel = new JPanel();
		JPanel bJPanel = new JPanel();
		tJPanel.setLayout(new BorderLayout());
		bJPanel.setLayout(new BorderLayout());

		final DefaultTableModel model = new DefaultTableModel();
		model.setDataVector(rowData, columnData);
		// 土壤深度下拉框
		final JComboBox<String> jcb = new JComboBox<String>();
		jcb.setEditable(true);
		jcb.setForeground(Color.white);
		jcb.setForeground(Color.BLACK);
		jcb.addItem("5");
		jcb.addItem("10");
		jcb.addItem("15");
		jcb.addItem("25");
		jcb.addItem("35");
		jcb.addItem("55");
		jcb.addItem("75");
		jcb.addItem("100");

		jcb.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String s = jcb.getSelectedItem().toString();
				if (s.equals(""))
					return;
				boolean flag = true;
				for (int i = 0; i < jcb.getItemCount(); i++) {
					if (jcb.getItemAt(i).equals(s)) {
						flag = false;
						break;
					}
				}
				if (flag)
					jcb.addItem(s);
			}
		});
		final JTable table = new JTable(model);
		table.getColumnModel().getColumn(2)
				.setCellEditor(new DefaultCellEditor(jcb));
		table.getColumnModel().getColumn(2).setCellRenderer(new MyCellRender());
		table.setRowHeight(25);
		table.setFont(new Font("kk", Font.PLAIN, 15));
		table.getTableHeader().setFont(new Font("", Font.PLAIN, 14));
		JTableHeader header = table.getTableHeader();
		table.getColumnModel().getColumn(0).setPreferredWidth(15);
		table.getColumnModel().getColumn(1).setPreferredWidth(15);
		table.getColumnModel().getColumn(2).setPreferredWidth(15);
		DefaultTableCellRenderer renderer = new DefaultTableCellRenderer();
		renderer.setHorizontalAlignment(DefaultTableCellRenderer.CENTER);
		table.setDefaultRenderer(Object.class, renderer);

		header.setPreferredSize(new Dimension(header.getWidth(), 30));
		table.setPreferredScrollableViewportSize(new Dimension(410, 380));
		table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		JScrollPane scroll = new JScrollPane(table);
		scroll.setSize(180, 100);
		tJPanel.add(scroll);

		JButton okbnt = new JButton("确定");
		okbnt.setLocation(400, 500);
		okbnt.setSize(10, 10);
		okbnt.setForeground(Color.blue);
		okbnt.setBackground(Color.white);
		bJPanel.add(okbnt);

		okbnt.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub

				// TODO Auto-generated method stub
				// 判断所选择的是哪种传感类型后进行存值
				if (SOILVWATER.equals(type)) {
					int act_soilVwater = 1;
					Connection conn = null;
					PreparedStatement pStatement = null;
					String sql = "insert into soilVW_sensor values(?,?,?,?,?,?,?);";
					/**
					 * 通过a,b,c的值计算体积含水量后将所有属性值存入数据表
					 */
					int cal_soilVwater_0 = Integer.parseInt(textField_a
							.getText())
							* (act_soilVwater ^ 2)
							+ Integer.parseInt(textField_b.getText())
							* act_soilVwater
							+ Integer.parseInt(textField_c.getText());

					String cal_soilVwater = String.valueOf(cal_soilVwater_0);
					try {
						for (int i = 0; i < Integer.parseInt(textField_1
								.getText()); i++) {
							idList.add(table.getValueAt(i, 1));
							groupList.add(table.getValueAt(i, 0));
							depthList.add(table.getValueAt(i, 2));
						}
						for (int i = 0; i < idList.size(); i++) {
							pStatement = conn.prepareStatement(sql);
							pStatement.setString(1, idList.get(i).toString());
							pStatement
									.setString(2, depthList.get(i).toString());
							pStatement
									.setString(4, groupList.get(i).toString());
							pStatement.setString(3, "控制区名称 ");
							pStatement.setString(5, "传感器名称");
							pStatement.setString(6,
									String.valueOf(act_soilVwater));
							pStatement.setString(7, cal_soilVwater);
							pStatement.executeQuery();
						}
						pStatement.close();

					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				} else if (SOILWJZS.equals(type)) {
					int act_soilWjzs = 1;
					Connection conn = null;
					PreparedStatement pStatement = null;
					String sql = "insert into soilWjzs_sensor values();";
					/**
					 * 通过a,b,c的值计算土壤水基质势后将所有属性值存入数据表
					 */
					int cal_soilWjzs_0 = Integer.parseInt(textField_a.getText())
							* (act_soilWjzs ^ 2)
							+ Integer.parseInt(textField_b.getText())
							* act_soilWjzs
							+ Integer.parseInt(textField_c.getText());

					String cal_soilWjzs = String.valueOf(cal_soilWjzs_0);
					try {
						for (int i = 0; i < Integer.parseInt(textField_1
								.getText()); i++) {
							idList.add(table.getValueAt(i, 1));
							groupList.add(table.getValueAt(i, 0));
							depthList.add(table.getValueAt(i, 2));
						}
						for (int i = 0; i < idList.size(); i++) {
							pStatement = conn.prepareStatement(sql);
							pStatement.setString(1, idList.get(i).toString());
							pStatement
									.setString(2, depthList.get(i).toString());
							pStatement
									.setString(4, groupList.get(i).toString());
							pStatement.setString(3, "控制区名称 ");
							pStatement.setString(5, "传感器名称");
							pStatement.setString(6,
									String.valueOf(act_soilWjzs));
							pStatement.setString(7, cal_soilWjzs);
							pStatement.executeQuery();
						}
						pStatement.close();

					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				} else if (SOILWSALT.equals(type)) {
					int act_soilVwater = 1;
					Connection conn = null;
					PreparedStatement pStatement = null;
					String sql = "insert into soilwSalt_sensor values();";
					/**
					 * 通过a,b,c的值计算体积含水量后将所有属性值存入数据表
					 */
					int cal_soilVwater_0 = Integer.parseInt(textField_a
							.getText())
							* (act_soilVwater ^ 2)
							+ Integer.parseInt(textField_b.getText())
							* act_soilVwater
							+ Integer.parseInt(textField_c.getText());
					String cal_soilVwater = String.valueOf(cal_soilVwater_0);
					try {
						for (int i = 0; i < Integer.parseInt(textField_1
								.getText()); i++) {
							idList.add(table.getValueAt(i, 1));
							groupList.add(table.getValueAt(i, 0));
							depthList.add(table.getValueAt(i, 2));
						}
						for (int i = 0; i < idList.size(); i++) {
							pStatement = conn.prepareStatement(sql);
							pStatement.setString(1, idList.get(i).toString());
							pStatement
									.setString(2, depthList.get(i).toString());
							pStatement
									.setString(4, groupList.get(i).toString());
							pStatement.setString(3, "控制区名称 ");
							pStatement.setString(5, "传感器名称");
							pStatement.setString(6,
									String.valueOf(act_soilVwater));
							pStatement.setString(7, cal_soilVwater);
							pStatement.executeQuery();
						}
						pStatement.close();

					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}

			}
		});

		frame1.getContentPane().add(tJPanel, BorderLayout.CENTER);
		frame1.getContentPane().add(bJPanel, BorderLayout.SOUTH);
		frame1.setVisible(true);
		frame1.setSize(450, 520);
		frame1.setLocationRelativeTo(null);
		frame1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}

	static class MyCellRender extends DefaultTableCellRenderer {

		public MyCellRender() {
		}

		@Override
		public Component getTableCellRendererComponent(JTable table,
				Object value, boolean isSelected, boolean hasFocus, int row,
				int column) {

			JComboBox<String> cBox = new JComboBox<>();
			cBox.setOpaque(true);
			cBox.setEditable(true);
			cBox.setForeground(Color.white);
			cBox.setForeground(Color.BLACK);
			return cBox;
		}
	}
}